﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data;
using System.Data.SqlClient;

namespace DAO
{
    public class TuaDeSachDAO
    {
        #region Them
        public static bool ThemTuaDeSach(TuaDeSachDTO Tuadesach)
        {
            bool result = false;
            try
            {
                // create list sqlparameter
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaTuaDe", Tuadesach.MaTuaDe));
                sqlParams.Add(new SqlParameter("@TenTuaDe", Tuadesach.TenTuaDe));
                sqlParams.Add(new SqlParameter("@MaLoaiSach", Tuadesach.MaLoaiSach));

                // call equivalent stored procedure
                int n = SqlDataAccessHelper.ExecuteNoneQuery("usp_InsertTUADESACH", sqlParams);
                if (n == 1)
                    result = true;
            }
            catch (System.Exception ex)
            {
            	throw ex;
            }
            return result;
        }
        #endregion
        #region Xoa
        public static bool XoaTuaDeSachByID(String matuade)
        {
            bool result = false;
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaTuaDe", matuade));

                int n = SqlDataAccessHelper.ExecuteNoneQuery("usp_DeleteTUADESACH", sqlParams);
                if (n == 1)
                    result = true;

            }
            catch (System.Exception ex)
            {
            	throw ex;
            }
            return result;
        }
        #endregion
        #region Sua
        public static bool SuaTuaDeSach(TuaDeSachDTO tuadesach)
        {
            bool result = false;
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaTuaDe", tuadesach.MaTuaDe));
                sqlParams.Add(new SqlParameter("@TenTuaDe", tuadesach.TenTuaDe));
                sqlParams.Add(new SqlParameter("@MaLoaiSach", tuadesach.MaLoaiSach));

                int n = SqlDataAccessHelper.ExecuteNoneQuery("usp_UpdateTUADESACH", sqlParams);
                if (n == 1)
                    result = true;
            }
            catch (System.Exception ex)
            {
            	throw ex;
            }
            return result;
        }
        #endregion
        #region Tim kiem
        public static TuaDeSachDTO TimTuaDeSachBangID(String matuade)
        {
            TuaDeSachDTO result = new TuaDeSachDTO();
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaTuaDe", matuade));

                DataTable dt = SqlDataAccessHelper.ExecuteQuery("usp_SelectTUADESACH", sqlParams);
                DataRow dr = dt.Rows[0];

                result.MaTuaDe = dr["MaTuaDe"].ToString();
                result.TenTuaDe = dr["TenTuaDe"].ToString();
                result.MaLoaiSach = dr["MaLoaiSach"].ToString();
            }
            catch (System.Exception ex)
            {
            	throw ex;
            }
            return result;
        }
        public static DataTable TimTatCaTuaDeSach()
        {
            DataTable dt= new DataTable();
            try
            {
                dt = SqlDataAccessHelper.ExecuteQuery("usp_SelectTUADESACHesAll");
              
            }
            catch (System.Exception ex)
            {
            	throw ex;
            }
            return dt;
        }
        public static List<TuaDeSachDTO> TimTuaDeSachBangLoaiSach(String maloaisach)
        {
            List<TuaDeSachDTO> result = new List<TuaDeSachDTO>();
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaLoaiSach", maloaisach));

                DataTable dt = SqlDataAccessHelper.ExecuteQuery("usp_SelectTUADESACHesByAndMaLoaiSach", sqlParams);

                foreach (DataRow dr in dt.Rows)
                {
                    TuaDeSachDTO tmp = new TuaDeSachDTO();

                    tmp.MaTuaDe = dr["MaTuaDe"].ToString();
                    tmp.TenTuaDe = dr["TenTuaDe"].ToString();
                    tmp.MaLoaiSach = dr["MaLoaiSach"].ToString();

                    result.Add(tmp);
                }
            }
            catch (System.Exception ex)
            {
            	throw ex;
            }
            return result;
        }
        public static List<TuaDeSachDTO> TimTuaDeSachBangTen(String tentuade)
        {
            List<TuaDeSachDTO> result = new List<TuaDeSachDTO>();
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@TenTuaDe", tentuade));

                DataTable dt = SqlDataAccessHelper.ExecuteQuery("TimTuaDeSachBangTen", sqlParams);
                foreach (DataRow dr in dt.Rows)
                {
                    TuaDeSachDTO tmp = new TuaDeSachDTO();

                    tmp.MaTuaDe = dr["MaTuaDe"].ToString();
                    tmp.TenTuaDe = dr["TenTuaDe"].ToString();
                    tmp.MaLoaiSach = dr["MaLoaiSach"].ToString();

                    result.Add(tmp);
                }
            }
            catch (System.Exception ex)
            {
            	throw ex;
            }
            return result;
        }
        public static bool CheckTuaDeSachBangID(String matuade)
        {
            bool result = false;
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaTuaDe", matuade));

                DataTable dt = SqlDataAccessHelper.ExecuteQuery("usp_SelectTUADESACH", sqlParams);
                if (dt.Rows.Count > 0)
                {
                    result = true;
                }
            }
            catch (System.Exception ex)
            {
            	throw ex;
            }
            return result;
        }
        #endregion
    }
}
